package com.voicechat.firstmet.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.voicechat.firstmet.R;
import com.voicechat.firstmet.base.BaseAdapter;
import com.voicechat.firstmet.bean.ShareGirlsBean;
import com.voicechat.firstmet.client.OssConfig;
import com.voicechat.firstmet.utils.GlideLoadUtils;
import com.voicechat.firstmet.utils.Utils;
import com.voicechat.firstmet.view.CircleImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @ProjectName: FirstMet
 * @Package: com.voicechat.firstmet.adapter
 * @ClassName: ShareGirlsAdapter
 * @Description: 共享闺蜜adapter
 * @Author: ljp
 * @CreateDate: 2019/1/17 10:38
 * @Version: 1.0
 */
public class ShareGirlsAdapter extends BaseAdapter<ShareGirlsBean.PageBean.ListBean> {
    private ItemClick itemClick;
    private List<String> sb;
    private String TAG = "ShareGirlsAdapter";
    String flag;
    public void setItemClick(ItemClick itemClick) {
        this.itemClick = itemClick;
    }

    /**
     * @param context activity_imessage
     * @param tlist   集合
     * @param mLayout 主布局
     */
    public ShareGirlsAdapter(Context context, List<ShareGirlsBean.PageBean.ListBean> tlist, int mLayout,String flag) {
        super(context, tlist, mLayout);
        sb = new ArrayList<>();
        this.flag = flag;
    }

    @Override
    protected RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return null;
    }

    @Override
    protected RecyclerView.ViewHolder getItemViewHolder(View view) {
        return new ItemViewHolder(view);
    }

    @Override
    protected RecyclerView.ViewHolder getFooterViewHolder(View view) {
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        if(viewHolder instanceof ItemViewHolder){
            ItemViewHolder itemViewHolder = (ItemViewHolder) viewHolder;
            ShareGirlsBean.PageBean.ListBean listBean = tlist.get(position);
            Log.i(TAG, "onBindViewHolder: --------"+listBean.toString());
            if(flag.equals("me")) {
                if (listBean.getStatus() == 0) {//0审核中 1审核通过 2驳回
                    itemViewHolder.relaAuditStatus.setVisibility(View.VISIBLE);
                    itemViewHolder.tvAudit.setText("审核中");
                } else if (listBean.getStatus() == 2) {
                    itemViewHolder.relaAuditStatus.setVisibility(View.VISIBLE);
                    itemViewHolder.tvAudit.setText("未通过");
                } else {
                    itemViewHolder.relaAuditStatus.setVisibility(View.GONE);
                }
            }
            if(!TextUtils.isEmpty(listBean.getAvatarUrl())) {
                String[] strings = Utils.spitPictureUrl(listBean.getAvatarUrl());
                if (!strings[0].equals(itemViewHolder.imageCover.getTag(R.id.image_cover))) {
                    // 加载图片
                    itemViewHolder.imageCover.setTag(R.id.image_cover, strings[0]);
                    ViewGroup.LayoutParams layoutParams = itemViewHolder.imageCover.getLayoutParams();
                    layoutParams.width = (Utils.getSrecchWidth(context)/2);
                    itemViewHolder.imageCover.setLayoutParams(layoutParams);
                    GlideLoadUtils.getInstance().glideLoad(context, OssConfig.OSS_HTTP + strings[0],
                            itemViewHolder.imageCover);
                    if(listBean.isSelectDelete() == 0 ){//是删除还是选定 -1选定  1删除 0取消选定
                        itemViewHolder.imageSelect.setVisibility(View.GONE);
                    }else if(listBean.isSelectDelete() == -1){
                        itemViewHolder.imageSelect.setVisibility(View.VISIBLE);
                        itemViewHolder.imageSelect.setImageResource(R.mipmap.share_un_delete);
                    }else {
                        itemViewHolder.imageSelect.setVisibility(View.VISIBLE);
                        itemViewHolder.imageSelect.setImageResource(R.mipmap.share_select_delete);
                    }
                }else {
                    if(listBean.isSelectDelete() == 0 ){//是删除还是选定 -1选定  1删除 0取消选定
                        itemViewHolder.imageSelect.setVisibility(View.GONE);
                    }else if(listBean.isSelectDelete() == -1){
                        itemViewHolder.imageSelect.setVisibility(View.VISIBLE);
                        itemViewHolder.imageSelect.setImageResource(R.mipmap.share_un_delete);
                    }else {
                        itemViewHolder.imageSelect.setVisibility(View.VISIBLE);
                        itemViewHolder.imageSelect.setImageResource(R.mipmap.share_select_delete);
                    }
                }
            }
            if (listBean.getGender() == 1) {//判断性别  1男 2女
                itemViewHolder.imageSex.setImageResource(R.mipmap.boy);
                itemViewHolder.linearSexAge.setBackgroundResource(R.mipmap.sex_blue);
            } else if (listBean.getGender() == 2) {
                itemViewHolder.imageSex.setImageResource(R.mipmap.girl);
                itemViewHolder.linearSexAge.setBackgroundResource(R.mipmap.sex_red);
            }
            itemViewHolder.tvName.setText(listBean.getNickName());
            itemViewHolder.tvAddress.setText(listBean.getAddress());
            //if (!TextUtils.isEmpty(listBean.getAge())) {
             //   String substring = listBean.getBirthday().substring(0, 4);
             //   String time = Utils.getStringTime(new Date());
              //  int age = (Integer.parseInt(time)) - (Integer.parseInt(substring));
            itemViewHolder.tvAge.setText(listBean.getAge()+"");
            itemViewHolder.view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(listBean.isSelectDelete() == 0 ){//是删除还是选定 -1选定  1删除 0取消选定
                        itemViewHolder.imageSelect.setVisibility(View.GONE);
                        itemClick.setItemClick(position,listBean);
                    }else if(listBean.isSelectDelete() == -1){
                        itemViewHolder.imageSelect.setVisibility(View.VISIBLE);
                        itemViewHolder.imageSelect.setImageResource(R.mipmap.share_select_delete);
                        listBean.setSelectDelete(1);
                        sb.add(listBean.getId());
                    }else {
                        itemViewHolder.imageSelect.setVisibility(View.VISIBLE);
                        itemViewHolder.imageSelect.setImageResource(R.mipmap.share_un_delete);
                        listBean.setSelectDelete(-1);
                        if(sb.contains(listBean.getId())){
                            sb.remove(listBean.getId());
                        }
                    }
                }
            });
            //}
        }

    }
    public List<String> getSb() {
        return sb;
    }
    class ItemViewHolder extends BaseViewHolder{
        @BindView(R.id.image_cover)
        CircleImageView imageCover;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.image_sex)
        ImageView imageSex;
        @BindView(R.id.tv_age)
        TextView tvAge;
        @BindView(R.id.linear_sex_age)
        LinearLayout linearSexAge;
        @BindView(R.id.image_vip)
        ImageView imageVip;
        @BindView(R.id.tv_address)
        TextView tvAddress;
        @BindView(R.id.image_un_select)
        ImageView imageSelect;
        //审核状态
        @BindView(R.id.rela_auditStatus)
        RelativeLayout relaAuditStatus;
        @BindView(R.id.tv_auditStatus)
        TextView tvAudit;

        View view;
        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
           view = itemView;
        }
    }

    public interface ItemClick{
        void setItemClick(int position,ShareGirlsBean.PageBean.ListBean listBean);
    }
}
